package ezy.lite.util;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

public class FragmentUtil {

    public static <T extends Fragment> T  newInstance(Class<T> clazz, Bundle args) {
        try {
            T fragment = clazz.newInstance();
            fragment.setArguments(args);
            return fragment;
        } catch (java.lang.InstantiationException e) {
        } catch (IllegalAccessException e) {
        }
        return null;
    }
    public static Fragment showTab(FragmentActivity activity, int id, Fragment hide, Fragment show) {
        FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
        if (hide != null) {
            ft.hide(hide);
        }
        if(!show.isAdded()) {
            ft.add(id, show);
        }
        ft.show(show);
        ft.commit();
        return show;
    }
    public static void push(FragmentActivity activity, int id, Fragment fragment) {
        FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
        ft.add(id, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.addToBackStack(null);
        ft.commit();
    }
    public static void push(FragmentActivity activity, int id, Class<? extends Fragment> clazz, Bundle args) {
        push(activity, id, newInstance(clazz, args));
    }
    public static void push(FragmentActivity activity, int id, Class<? extends Fragment> clazz) {
        push(activity, id, newInstance(clazz, null));
    }
    public static void add(FragmentActivity activity, int id, Fragment fragment) {
        FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
        ft.add(id, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.commit();
    }
    public static void add(FragmentActivity activity, int id, Class<? extends Fragment> clazz, Bundle args) {
        add(activity, id, newInstance(clazz, args));
    }
    public static void add(FragmentActivity activity, int id, Class<? extends Fragment> clazz) {
        add(activity, id, newInstance(clazz, null));
    }

    public static void replace(FragmentActivity activity, int id, Fragment fragment) {
        FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.replace(id, fragment);
        ft.commit();
    }
    public static void replace(FragmentActivity activity, int id, Class<? extends Fragment> clazz, Bundle args) {
        replace(activity, id, newInstance(clazz, args));
    }
    public static void replace(FragmentActivity activity, int id, Class<? extends Fragment> clazz) {
        replace(activity, id, newInstance(clazz, null));
    }

    public static <T extends Fragment> T find(FragmentActivity activity, int resId) {
        return (T)activity.getSupportFragmentManager().findFragmentById(resId);
    }

    public static <T extends Fragment> T find(FragmentActivity activity, String tag) {
        return (T)activity.getSupportFragmentManager().findFragmentByTag(tag);
    }
}